<?php

namespace App\Http\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;


class NationGrid extends Base
{
    protected $table = 'nation_grid_log';
    protected $user_liang_inc_cache = 'NATION:GRID:LIANG:USER';
    protected $all_liang_inc_cache = 'NATION:GRID:LIANG:ALL';
    protected $liang_cache = 'NATION:LIANG:NUM';

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }

    public function getInfo($user_id)
    {
        $where['user_id'] = $user_id;
        $data = DB::table($this->table)
            ->where($where)
            ->first();
        return self::objToArray($data);
    }

    public function updateLog($user_id, $feild)
    {
        if (!$feild) {
            return false;
        }
        $where['user_id'] = $user_id;
        $date[$feild] = 1;
        return DB::table($this->table)->where($where)->update($date);

    }

    public function incNum($user_id, $num)
    {
        $where['user_id'] = $user_id;
        return DB::table($this->table)->where($where)->increment('ticket_num', $num);
    }

    public function createTicket($user_id, $num)
    {
        $data['user_id'] = $user_id;
        $data['ticket_num'] = $num;
        $data['consumer_num'] = 0;
        return DB::table($this->table)->insertGetId($data);
    }

    public function decrementNum($user_id, $num)
    {
        $where['user_id'] = $user_id;
        DB::table($this->table)->where($where)->increment('consumer_num', $num);
        return DB::table($this->table)->where($where)->decrement('ticket_num', $num);
    }

    public function getUserIncLiang($user_id)
    {
        $user_key = $this->user_liang_inc_cache;
        return Redis::hincrby($user_key, $user_id, 1);
    }

    public function getAllIncLiang()
    {
        $all_key = $this->all_liang_inc_cache;
        return Redis::incrby($all_key, 1);
    }

    public function insertAllData($data)
    {
        $result = DB::table('nation_mall_log')->insert($data);
        return $result;
    }

    public function popLiang()
    {
        return Redis::lpop($this->liang_cache);
    }

    public function getMalllog($user_id, $gift_type = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
    {
        $where['user_id'] = $user_id;
        $result = DB::table('nation_mall_log')->where($where)->whereIn('gift_type', $gift_type)
            ->select('user_id', DB::raw('SUM(num_count) as num_count'), DB::raw('SUM(gift_value) as gift_value'), 'gift_type')
            ->groupBy('gift_type')
            ->orderBy('gift_type', 'asc')
            ->get();
        return $this->objToArray($result);
    }

    //添加兑换记录
    public function createConvertFieldFlag($user_id, $num)
    {
        $data['user_id'] = $user_id;
        $data['consumer_num'] = $num;
        return DB::table('convert_field_flag')->insert($data);
    }

    //获取兑换记录
    public function getConvertFieldFlag($user_id)
    {
        $where['user_id'] = $user_id;
        return DB::table('convert_field_flag')->where($where)->sum('consumer_num');

    }

    public function getLog($user_id)
    {
        $res = DB::table('nation_mall_log')->where(['user_id' => $user_id])->orderBy('id', 'desc')->get();
        return $this->objToArray($res);
    }


    public function buyTicketLog($user_id,$num)
    {
        $data['user_id'] = $user_id;
        $data['buy_num'] = $num;
        return DB::table('buy_ticket_log')->insert($data);
    }


}
